package com.zhentao.kafka;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.model.article.pojo.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleDownOrUpListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "articleDownOrUpTopic")
    public void articleDownOrUp(String data){
        System.out.println("获取kafka中的数据"+data);
        if(StringUtils.isNotBlank(data)){
            Map map = JSON.parseObject(data, Map.class);
            //修改文章的状态
            QueryWrapper<ApArticleConfig> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(ApArticleConfig::getArticleId, map.get("articleId"));
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(wrapper);
            int enable = (int) map.get("enable");
            boolean enabl = true;
            if(enable==1){
                enabl=false;
            }
            apArticleConfig.setIsDown(enabl);
            apArticleConfigMapper.updateById(apArticleConfig);
        }
    }
}
